* Preliminary computations

*===============================================================================
* Drop unused factors and sign factors based on original studies
*===============================================================================

* Monthly

use "~/Dropbox/Research/Data/CRSP-Compustat/Computations/fmfactors.dta", clear

keep yyyymm factorid factorname factorret factorret_in

* drop unused
foreach var in divmonth sharevolume coskewness divyield organizationcapital advertising opleverage rd tax cbop disttohigh maxdailyret momentum intmomentum momreversal herfindahl qmj_profitability seasonality strev {
	drop if factorname=="`var'"
	}
	
* flip signs
foreach var in marketbeta firmage idiosyncraticvol ltrev nomprice accruals assetgrowth debtissuance enterprisemultiple inventorygrowth netoperatingassets abnormalinvestment indadjcapxgrowth netwcap_changes oscore salesgrowth shareissuance1 shareissuance5 sustainablegrowth total_xfin investmenttocapital invgrowthrate investmenttoassets chs_distress {
	qui replace factorret = -factorret if factorname=="`var'"
	qui replace factorret_in = -factorret_in if factorname=="`var'"
	}
	
* renumber
gen factorid2 = .
local ctr = 0

foreach var in size booktomarket ffprofitability assetgrowth ltrev idiosyncraticvol qmj marketbeta amihud firmage nomprice gkm_voldist grossprofitability returnonequity profitability profitmargin aturnoverchg accruals netoperatingassets netwcap_changes cashflowtoequity earningsprice enterprisemultiple salestoprice inventorygrowth salesgrowth sustainablegrowth salesminusinventory invgrowthrate abnormalinvestment indadjcapxgrowth investmenttocapital investmenttoassets debtissuance leverage shareissuance1 shareissuance5 total_xfin oscore zscore chs_distress piotroskiF accruals_beme  {

	local ctr = `ctr' + 1
	qui replace factorid2 = `ctr' if factorname=="`var'"
	
	}

qui replace factorid = factorid2
drop factorid2	
	
keep if yyyymm>=196307 
save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_clean.dta", replace


* Daily

use "~/Dropbox/Research/Data/CRSP-Compustat/Computations/fmfactors_daily.dta", clear

* drop unused
foreach var in divmonth sharevolume coskewness divyield organizationcapital advertising opleverage rd tax cbop disttohigh maxdailyret momentum intmomentum momreversal herfindahl qmj_profitability seasonality strev {
	drop if factorname=="`var'"
	}
	
* flip signs
foreach var in marketbeta firmage idiosyncraticvol ltrev nomprice accruals assetgrowth debtissuance enterprisemultiple inventorygrowth netoperatingassets abnormalinvestment indadjcapxgrowth netwcap_changes oscore salesgrowth shareissuance1 shareissuance5 sustainablegrowth total_xfin investmenttocapital invgrowthrate investmenttoassets chs_distress {
	qui replace factorret = -factorret if factorname=="`var'"
	qui replace factorret_in = -factorret_in if factorname=="`var'"
	}
	
* renumber
gen factorid2 = .
local ctr = 0

foreach var in size booktomarket ffprofitability assetgrowth ltrev idiosyncraticvol qmj marketbeta amihud firmage nomprice gkm_voldist grossprofitability returnonequity profitability profitmargin aturnoverchg accruals netoperatingassets netwcap_changes cashflowtoequity earningsprice enterprisemultiple salestoprice inventorygrowth salesgrowth sustainablegrowth salesminusinventory invgrowthrate abnormalinvestment indadjcapxgrowth investmenttocapital investmenttoassets debtissuance leverage shareissuance1 shareissuance5 total_xfin oscore zscore chs_distress piotroskiF accruals_beme  {

	local ctr = `ctr' + 1
	qui replace factorid2 = `ctr' if factorname=="`var'"
	
	}

qui replace factorid = factorid2
drop factorid2			
	
keep if 100*year(date)+month(date)>=196307 
keep if ~missing(date)
save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_daily_clean.dta", replace



*===============================================================================
*
* Table 1: List of factors
*
*===============================================================================
	
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_clean.dta", clear

qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors_2021dec.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 

qui statsby _b _se, by(factorid) saving("~/Documents/CRSP-Compustat/standard_ff3.dta", replace) nodots: regress factorret mktrf smb hml
qui statsby _b _se, by(factorid) saving("~/Documents/CRSP-Compustat/industryneutral_ff3.dta", replace) nodots: regress factorret_in mktrf smb hml

collapse (mean) factorret factorret_in (semean) se_factorret=factorret se_factorret_in=factorret_in (first) factorname (min) min_yyyymm=yyyymm (max) max_yyyymm=yyyymm, by(factorid)

gen t_avg = factorret / se_factorret
gen t_in_avg = factorret_in / se_factorret_in
drop se_*

replace factorret = 100 * factorret
replace factorret_in = 100 * factorret_in

merge 1:1 factorid using "~/Documents/CRSP-Compustat/standard_ff3.dta", nogenerate keepusing(_b_cons _se_cons)

gen t_alpha = _b_cons / _se_cons 
gen alpha = 100 * _b_cons

drop _b_cons _se_cons

merge 1:1 factorid using "~/Documents/CRSP-Compustat/industryneutral_ff3.dta", nogenerate keepusing(_b_cons _se_cons)
	
gen t_in_alpha = _b_cons / _se_cons 
gen alpha_in = 100 * _b_cons

drop _b_cons _se_cons

order factorid factorname min_yyyymm max_yyyymm factorret t_avg factorret_in t_in_avg alpha t_alpha alpha_in t_in_alpha


*===============================================================================
*
* Table A1: List of factors with Fama-French five-factor model alphas (APPENDIX)
*
*===============================================================================
	
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_clean.dta", clear

qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors_2021dec.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 

qui statsby _b _se, by(factorid) saving("~/Documents/CRSP-Compustat/standard_ff3.dta", replace) nodots: regress factorret mktrf smb hml cma umd
qui statsby _b _se, by(factorid) saving("~/Documents/CRSP-Compustat/industryneutral_ff3.dta", replace) nodots: regress factorret_in mktrf smb hml cma umd

collapse (mean) factorret factorret_in (semean) se_factorret=factorret se_factorret_in=factorret_in (first) factorname (min) min_yyyymm=yyyymm (max) max_yyyymm=yyyymm, by(factorid)

gen t_avg = factorret / se_factorret
gen t_in_avg = factorret_in / se_factorret_in
drop se_*

replace factorret = 100 * factorret
replace factorret_in = 100 * factorret_in

merge 1:1 factorid using "~/Documents/CRSP-Compustat/standard_ff3.dta", nogenerate keepusing(_b_cons _se_cons)

gen t_alpha = _b_cons / _se_cons 
gen alpha = 100 * _b_cons

drop _b_cons _se_cons

merge 1:1 factorid using "~/Documents/CRSP-Compustat/industryneutral_ff3.dta", nogenerate keepusing(_b_cons _se_cons)
	
gen t_in_alpha = _b_cons / _se_cons 
gen alpha_in = 100 * _b_cons

drop _b_cons _se_cons

order factorid factorname min_yyyymm max_yyyymm factorret t_avg factorret_in t_in_avg alpha t_alpha alpha_in t_in_alpha




*===============================================================================
* Calculation/Processing: Scale factors to 10% volatility
*===============================================================================

use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_clean.dta", clear

gen long monthindex = (trunc(yyyymm/100) - 1900) * 12 + mod(yyyymm,100)

unique monthindex
local Nmonths = `r(unique)'-1

foreach var of varlist factorret factorret_in {
	
	xtset factorid monthindex
	
	forvalues j=1/`Nmonths' {
		qui gen lagged`j' = L`j'.`var'
		}
	
	qui egen sd = rowsd(lagged*)
	qui egen n = rownonmiss(lagged*)

	qui replace sd = . if n<12
	
	qui replace `var' = `var' * (0.1 / sqrt(12)) / sd

	drop lagged* sd n
	
	}

keep factorret factorret_in factorname yyyymm factorid monthindex

drop if missing(factorret) & missing(factorret_in)

xtset factorid monthindex

* 60-month rolling 
preserve

qui rolling mean=r(mean), window(60) clear: summarize factorret
keep factorid end mean 
rename end monthindex
save "~/Documents/tmp_means.dta", replace

restore

* expanding window rolling 
preserve

qui rolling mean=r(mean), window(60) recursive clear: summarize factorret
keep factorid end mean 
rename end monthindex
rename mean mean2
save "~/Documents/tmp_means2.dta", replace

restore


merge 1:1 factorid monthindex using "~/Documents/tmp_means.dta", nogenerate
merge 1:1 factorid monthindex using "~/Documents/tmp_means2.dta", nogenerate

gen factorret_adj = factorret - L.mean
gen factorret_adj2 = factorret - L.mean2


drop monthindex mean mean2

save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", replace
	
	
	
	
	

*===============================================================================
*
* Calculation/Processing: Create Industry momentum strategies
*
*===============================================================================

forvalues industryspec=1/3 {
	
	if `industryspec'==1 {
		local industryname = "mgindustry"
		}
	else if `industryspec'==2 {
		local industryname = "ffindustry49"
		}
	else if `industryspec'==3 {
		local industryname = "hpindustry25"
		}
		
	foreach L in "1" "6" "12" "13" {
		foreach H in "1" "6" {
			foreach pf in "1" "2" {
					
				use "~/Dropbox/Research/Data/CRSP-Compustat/Computations/industryreturns.dta", clear
					
				rename `industryname'ret industryret				
				
				keep dateindex industryid yyyymm industryret
				
				qui keep if yyyymm>=196307 

				* compute past returns
				qui xtset industryid dateindex

				* past if no skipping
				if `L'<=12 {
					local Lm = `L'-1
					forvalues l=0/`Lm' {
						qui gen lrets`l' = L`l'.industryret
						}
					}
				else { // skip one month (this is overloaded to do t-2 to t-12)
					local Lm = `L'-2
					forvalues l=1/`Lm' {
						qui gen lrets`l' = L`l'.industryret
						}
					}
					
				qui egen avgret = rowmean(lrets*)
				qui egen n = rownonmiss(lrets*)
				
				if `L'<=12 {
					qui replace avgret = . if n<`L'
					}
				else {
					qui replace avgret = . if n<11
					}
					
				drop n lrets*
				
				* assign industries into 3/3 portfolios
				if `pf'==1 {

					qui bysort yyyymm: egen r = rank(avgret)
					qui bysort yyyymm: egen n = max(r)

					qui drop if missing(n) | missing(r)
				
					qui gen nf = max(round(n*(3/20)),1)
					qui gen portfolio = 2
					qui replace portfolio = 1 if r<=nf
					qui replace portfolio = 3 if r>=n-(nf-1)				
					
					drop n r
					
					}
				else {
					
					qui bysort yyyymm: egen m = median(avgret)
					drop if missing(m,avgret)
					qui gen portfolio = 1 if avgret<m & ~missing(avgret,m)
					qui replace portfolio = 3 if avgret>m & ~missing(avgret,m)
					qui replace portfolio = 2 if avgret==m
					
					
					drop m
					
					}
				
				* compute future returns 
				
				forvalues lead=1/`H' {
				
					preserve
				
					qui xtset industryid dateindex
					qui gen retnm = F`lead'.industryret

					qui replace yyyymm = yyyymm + `lead'
					qui replace yyyymm = (trunc(yyyymm/100) + 1) * 100 + mod(yyyymm,100) - 12 if mod(yyyymm,100)>12
				
					collapse (mean) retnm, by(yyyymm portfolio)
					qui reshape wide retnm, i(yyyymm) j(portfolio)
				
					qui gen mo`lead' = retnm3 - retnm1
					drop retnm?
				
					if `H'>1 {
						qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/industrymom_tmp.dta", nogenerate
						}
					qui save "~/Documents/CRSP-Compustat/industrymom_tmp.dta", replace
					restore
				
					}

				use "~/Documents/CRSP-Compustat/industrymom_tmp.dta", clear
				qui egen m = rowmiss(mo?)

				qui egen double imomL`L'H`H'p`pf' = rowmean(mo?) if m==0

				keep yyyymm imomL`L'H`H'p`pf'			
			
				qui keep if ~missing(imomL`L'H`H'p`pf')
				
				gen industryname = "`industryname'"
				if `pf'>1 | `L'>1 | `H'>1 {
					qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/industrymomentum`industryspec'.dta", nogenerate
					}
				sort yyyymm
				qui save "~/Documents/CRSP-Compustat/industrymomentum`industryspec'.dta", replace
				
				}
			}
		}
	}
	
	
* merge results

use "~/Documents/CRSP-Compustat/industrymomentum1.dta", clear
keep yyyymm imomL1H1p2
rename imomL1H1p2 imom
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/industrymomentum2.dta", nogenerate keepusing(imomL1H1p2 imomL12H1p2)
rename imomL1H1p2 imom2
rename imomL12H1p2 imom2_12mo
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/industrymomentum3.dta", nogenerate keepusing(imomL1H1p2)
rename imomL1H1p2 imom3

qui merge 1:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", nogenerate keep(1 3) keepusing(mktrf smb hml rmw cma umd)
sort yyyymm
save "~/Documents/CRSP-Compustat/imom_etc.dta", replace


*===============================================================================
* Table 2: Industry momentum
* Table A4: Alternative specifications
*===============================================================================

* positions in all MG industries
estimates clear
local regrctr = 0

use "~/Documents/CRSP-Compustat/industrymomentum1.dta", clear
merge 1:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", nogenerate keep(1 3)

foreach L in "1" "6" "12" "66" {

	if `L'==66 {
		local L = 6
		local H = 6
		}
	else {
		local H = 1
		}

	qui regress imomL`L'H`H'p2
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
		
	qui regress imomL`L'H`H'p2 mktrf smb hml rmw cma
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
	
	qui regress imomL`L'H`H'p2 mktrf smb hml rmw cma umd
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'

	if `regrctr'==6 {
		esttab regr?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 
		local regrctr=0
		}
	
	}

* 3/3 industries, full sample
estimates clear
local regrctr = 0

foreach L in "1" "6" "12" "66" {

	if `L'==66 {
		local L = 6
		local H = 6
		}
	else {
		local H = 1
		}
		
	qui regress imomL`L'H`H'p1
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
		
	qui regress imomL`L'H`H'p1 mktrf smb hml rmw cma
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
	
	qui regress imomL`L'H`H'p1 mktrf smb hml rmw cma umd
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'

	if `regrctr'==6 {
		esttab regr?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 
		local regrctr=0
		}
		
	}
	
* 3/3 industries, Moskowitz-Grinblatt sample
estimates clear
local regrctr = 0

foreach L in "1" "6" "12" "66" {

	if `L'==66 {
		local L = 6
		local H = 6
		}
	else {
		local H = 1
		}
		
	qui regress imomL`L'H`H'p1 if yyyymm>=196307 & yyyymm<=199507
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
		
	qui regress imomL`L'H`H'p1 mktrf smb hml rmw cma if yyyymm>=196307 & yyyymm<=199507
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
	
	qui regress imomL`L'H`H'p1 mktrf smb hml rmw cma umd if yyyymm>=196307 & yyyymm<=199507
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'

	if `regrctr'==6 {
		esttab regr?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 
		local regrctr=0
		}
		
	}
	

	


 	
*===============================================================================
*
* Table 3: Cross-sectional factor momentum
* Table A6: Time-series factor momentum
*
*===============================================================================

local ctr = 0

foreach var in "factorret" "factorret_in" {
	
	use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", clear

	local ctr = `ctr'+1
	
	drop if missing(`var') 

	gen long monthindex = (trunc(yyyymm/100) - 1900) * 12 + mod(yyyymm,100)

	xtset factorid monthindex

	* time-series momentum	
	preserve
	
	gen `var'1yr = L1.`var' + L2.`var' + L3.`var' + L4.`var' + L5.`var' + L6.`var' + L7.`var' + L8.`var' + L9.`var' + L10.`var' + L11.`var' + L12.`var'
	
	gen     tsmom`ctr' =  `var' if `var'1yr>=0 & ~missing(`var'1yr)
	replace tsmom`ctr' = -`var' if `var'1yr<0 & ~missing(`var'1yr)
	
	collapse (mean) tsmom`ctr', by(yyyymm)
	
	qui save "~/Documents/tsmom_tmp", replace
	
	restore
	
	* cross-sectional momentum	
	gen `var'pm = L1.`var'

	bysort yyyymm: egen m = median(`var'pm)	
	gen     pos = 2 if `var'pm > m & ~missing(`var'pm,m)
	replace pos = 1 if `var'pm < m & ~missing(`var'pm,m)

	keep if ~missing(pos)

	collapse (mean) `var', by(yyyymm pos)

	reshape wide `var', i(yyyymm) j(pos)

	gen csfmom`ctr' = `var'2 - `var'1
	
	keep yyyymm csfmom`ctr'
	qui merge 1:1 yyyymm using "~/Documents/tsmom_tmp", nogenerate
	
	if `ctr'>1 {
		merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", nogenerate
		}
	save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", replace

	}
	
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", clear

* Merge in Fama-French factors
qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 

* Merge in q factors
qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/qfactors.dta", nogenerate

estimates clear

* cross-sectional factor momentum

qui regress csfmom1 
estimates store regr1
qui regress csfmom1 mktrf smb hml rmw cma
estimates store regr2
qui regress csfmom1 r_mkt r_me r_ia r_roe
estimates store regr3
qui regress csfmom1 mktrf smb hml rmw cma umd
estimates store regr4
qui regress csfmom1 mktrf smb hml rmw cma umd tsmom1
estimates store regr5

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 

* time-series factor momentum
estimates clear

qui regress tsmom1 
estimates store regr1
qui regress tsmom1 mktrf smb hml rmw cma
estimates store regr2
qui regress csfmom1 r_mkt r_me r_ia r_roe
estimates store regr3
qui regress tsmom1 mktrf smb hml rmw cma umd
estimates store regr4
qui regress tsmom1 mktrf smb hml rmw cma umd csfmom1
estimates store regr5

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 


*===============================================================================
* ASIDE:
*
* cross-sectional factor momentum, factor returns adjusted for long-term means
*
* keep a consistent sample!
*===============================================================================

use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", clear

drop if missing(factorret_adj2) 

gen long monthindex = (trunc(yyyymm/100) - 1900) * 12 + mod(yyyymm,100)

xtset factorid monthindex

* cross-sectional momentum	
gen Lret = L1.factorret_adj2

bysort yyyymm: egen m = median(Lret)	
gen     pos = 2 if Lret > m & ~missing(Lret,m)
replace pos = 1 if Lret < m & ~missing(Lret,m)

keep if ~missing(pos)

collapse (mean) factorret, by(yyyymm pos)

reshape wide factorret, i(yyyymm) j(pos)

gen csfmom_adj = factorret2 - factorret1

merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", keepusing(csfmom1 tsmom1) nogenerate

* Merge in Fama-French factors
qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 

* Merge in q factors
qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/qfactors.dta", nogenerate

estimates clear	
	
keep if ~missing(csfmom_adj)

foreach var in "csfmom1" "csfmom_adj" {
	qui regress `var'
	estimates store regr1
	qui regress `var' mktrf smb hml rmw cma
	estimates store regr2
	qui regress `var' r_mkt r_me r_ia r_roe
	estimates store regr3
	qui regress `var' mktrf smb hml rmw cma umd
	estimates store regr4
	qui regress `var' mktrf smb hml rmw cma umd tsmom1
	estimates store regr5

	esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 
}



*===============================================================================
*
* Computation/Processing: create Factor momentum with different holding periods
*
*===============================================================================
foreach L in "1" "6" "12" "13" {
	foreach H in "1" "6" {
				
		use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", clear
		
		gen long dateindex = (trunc(yyyymm/100)-1900) * 12 + mod(yyyymm,100)
		
		keep dateindex factorid yyyymm factorret
		
		qui keep if yyyymm>=196307 

		* compute past returns
		qui xtset factorid dateindex

		* past if no skipping
		if `L'<=12 {
			local Lm = `L'-1
			forvalues l=0/`Lm' {
				qui gen lrets`l' = L`l'.factorret
				}
			}
		else { // skip one month (this is overloaded to do t-2 to t-12)
			local Lm = `L'-2
			forvalues l=1/`Lm' {
				qui gen lrets`l' = L`l'.factorret
				}
			}
			
		qui egen avgret = rowmean(lrets*)
		qui egen n = rownonmiss(lrets*)
		
		if `L'<=12 {
			qui replace avgret = . if n<`L'
			}
		else {
			qui replace avgret = . if n<11
			}
			
		drop n lrets*
		
		* assign factors into portfolios
		qui bysort yyyymm: egen m = median(avgret)
		drop if missing(m,avgret)
		qui gen portfolio = 1 if avgret<m & ~missing(avgret,m)
		qui replace portfolio = 3 if avgret>m & ~missing(avgret,m)
		qui replace portfolio = 2 if avgret==m
		drop m
			
		* compute future returns 
		
		forvalues lead=1/`H' {
		
			preserve
		
			qui xtset factorid dateindex
			qui gen retnm = F`lead'.factorret

			qui replace yyyymm = yyyymm + `lead'
			qui replace yyyymm = (trunc(yyyymm/100) + 1) * 100 + mod(yyyymm,100) - 12 if mod(yyyymm,100)>12
		
			collapse (mean) retnm, by(yyyymm portfolio)
			qui reshape wide retnm, i(yyyymm) j(portfolio)
		
			qui gen mo`lead' = retnm3 - retnm1
			drop retnm?
		
			if `H'>1 {
				qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/factormom_tmp.dta", nogenerate
				}
			qui save "~/Documents/CRSP-Compustat/factormom_tmp.dta", replace
			restore
		
			}

		use "~/Documents/CRSP-Compustat/factormom_tmp.dta", clear
		qui egen m = rowmiss(mo?)

		qui egen double fmomL`L'H`H' = rowmean(mo?) if m==0

		keep yyyymm fmomL`L'H`H'			
	
		qui keep if ~missing(fmomL`L'H`H')
		
		if `L'>1 | `H'>1 {
			qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/factormomentumHL.dta", nogenerate
			}
		sort yyyymm
		qui save "~/Documents/CRSP-Compustat/factormomentumHL.dta", replace
		
		}
	}
	
	
*===============================================================================
* Table A5: Factor momentum with alternative holding and formation periods
*===============================================================================

estimates clear
local regrctr = 0

use "~/Documents/CRSP-Compustat/factormomentumHL.dta", clear
merge 1:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", nogenerate keep(1 3)

foreach L in "1" "6" "12" "13" "66" {

	if `L'==66 {
		local L = 6
		local H = 6
		local last=1
		}
	else {
		local H = 1
		local last=0
		}

	qui regress fmomL`L'H`H'
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
		
	qui regress fmomL`L'H`H' mktrf smb hml rmw cma
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'
	
	qui regress fmomL`L'H`H' mktrf smb hml rmw cma umd
	local regrctr = `regrctr'+1
	estimates store regr`regrctr'

	if `regrctr'==6 | `last'==1 {
		esttab regr?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 
		estimates clear
		local regrctr=0
		}
	
	}	
	
	


*===============================================================================
*
* Table 4: Industry momentum vs. factor momentum
*
*===============================================================================

* Standard- and industry-neutral factor momentum versus industry momentum

use "~/Documents/CRSP-Compustat/imom_etc.dta", clear
merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", nogenerate keepusing(csfmom?)

keep if ~missing(imom,csfmom1)

* Comparisons of factor momentums

estimates clear

qui regress csfmom1 mktrf smb hml rmw cma
estimates store regr1
qui regress csfmom1 mktrf smb hml rmw cma csfmom2
estimates store regr2
qui regress csfmom1 mktrf smb hml rmw cma imom
estimates store regr3

qui regress csfmom2 mktrf smb hml rmw cma
estimates store regr4
qui regress csfmom2 mktrf smb hml rmw cma csfmom1
estimates store regr5
qui regress csfmom2 mktrf smb hml rmw cma imom
estimates store regr6

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 


estimates clear

qui regress imom mktrf smb hml rmw cma 
estimates store regr1
qui regress imom mktrf smb hml rmw cma csfmom1
estimates store regr2
qui regress imom mktrf smb hml rmw cma csfmom2
estimates store regr3

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 



*===============================================================================
*
* Appendix Figure 1: Cumulative returns on industry and factor momentum
*
*===============================================================================

use "~/Documents/CRSP-Compustat/imom_etc.dta", clear
keep yyyymm imom

merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", nogenerate keepusing(csfmom?)

keep if ~missing(csfmom1)

* scale to 10% annual volatility
foreach var of varlist imom csfmom* {
	qui summ `var'
	qui replace `var' = (0.1 / sqrt(12)) * `var' / `r(sd)'
	}

* compute cumulative returns
foreach var of varlist imom csfmom* {

	sort yyyymm
	gen double c`var' = 1 * (1 + `var') if _n==1
	sort yyyymm
	qui replace c`var' = c`var'[_n-1] * (1 + `var') if _n>1
	
	}
	
keep yyyymm cimom ccsfmom1 ccsfmom2
order yyyymm cimom ccsfmom1 ccsfmom2
sort yyyymm
export delimited using "~/Dropbox/Research/Factor Momentum 2/Matlab/fm_timeseries.csv", replace delimiter(",")	
	
	

	
*===============================================================================
*
* Table 5: Factor momentum within the FF5 model and the full model
*
*===============================================================================

use "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", clear

keep yyyymm mktrf smb hml rmw cma
rename mktrf factor1
rename smb factor2
rename hml factor3
rename rmw factor4
rename cma factor5

keep if yyyymm>=196307

reshape long factor, i(yyyymm) j(factorid)

rename factor ret


gen long monthindex = (trunc(yyyymm/100) - 1900) * 12 + mod(yyyymm,100)

xtset factorid monthindex

* cross-sectional momentum	
gen Lret = L1.ret

bysort yyyymm: egen m = median(Lret)	
gen     pos = 2 if Lret > m & ~missing(Lret,m)
replace pos = 1 if Lret < m & ~missing(Lret,m)

keep if ~missing(pos)

collapse (mean) ret, by(yyyymm pos)

reshape wide ret, i(yyyymm) j(pos)

gen ff5fmom = ret2 - ret1
	
keep yyyymm ff5fmom

save "~/Documents/ff5fmom.dta", replace

	
use "~/Documents/ff5fmom.dta", clear

* Merge in Fama-French factors
qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 

** FF5 factor momentum

estimates clear

qui regress ff5fmom
estimates store regr1
qui regress ff5fmom mktrf smb hml rmw cma
estimates store regr2

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 

** Full factor momentum

use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", clear
keep factorid yyyymm factorret
reshape wide factorret, i(yyyymm) j(factorid)

qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", keep(1 3) keepusing(mktrf smb hml rmw cma umd) nogenerate 
merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", nogenerate keepusing(csfmom?)

qui regress csfmom1
estimates store regr1
regress csfmom1 factorret* mktrf smb hml rmw cma
estimates store regr2

esttab regr*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar 




	
*===============================================================================
*
* Table 6: Characteristics vs. factor momentum
*
*===============================================================================

use "~/Documents/CRSP-Compustat/imom_etc.dta", clear
merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/cstsmom.dta", nogenerate keepusing(csfmom?)
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/characteristicmomentum.dta", nogenerate
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/characteristicmomentum_big.dta", nogenerate
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/lewellenmomentum.dta", nogenerate

egen composite = rowmean(imom imom2 sz_mom bm_mom sz_bm_mom kmeans25 ward25 kmeans25_big ward25_big)

preserve
keep yyyymm imom imom2 sz_mom bm_mom sz_bm_mom kmeans25 ward25 kmeans25_big ward25_big composite
save "~/Documents/CRSP-Compustat/characteristicmomentum_strategies.dta", replace
restore

local ctr = 0

* correlations

corr imom imom2 sz_mom bm_mom sz_bm_mom kmeans25 ward25 kmeans25_big ward25_big csfmom1

* spanning regressions

foreach depvar of varlist imom imom2 sz_mom bm_mom sz_bm_mom kmeans25 ward25 kmeans25_big ward25_big composite { 
	
	preserve
	
	local ctr = `ctr' + 1
	
	* dependent variable: CHARACTERISTIC momentum 
	
	qui regress `depvar' mktrf smb hml rmw cma
	local alpha_ff5 = 100 * _b[_cons]
	local talpha_ff5 = _b[_cons] / _se[_cons]
	local r2_ff5 = `e(r2_a)'
	
	qui regress `depvar' mktrf smb hml rmw cma umd
	local alpha_ff5umd = 100 * _b[_cons]
	local talpha_ff5umd = _b[_cons] / _se[_cons]
	local r2_ff5umd = `e(r2_a)'

	qui regress `depvar' mktrf smb hml rmw cma umd imom
	local alpha_ff5imom = 100 * _b[_cons]
	local talpha_ff5imom = _b[_cons] / _se[_cons]
	local r2_ff5imom = `e(r2_a)'

	qui regress `depvar' mktrf smb hml rmw cma umd csfmom1
	local alpha_ff5fmom = 100 *  _b[_cons]
	local talpha_ff5fmom = _b[_cons] / _se[_cons]
	local r2_ff5fmom = `e(r2_a)'	
	
	local N = `e(N)'
	
	* dependent variable: FACTOR momentum 
	
	qui regress csfmom1 mktrf smb hml rmw cma umd `depvar'
	local alpha_ff5cmom = 100 * _b[_cons]
	local talpha_ff5cmom = _b[_cons] / _se[_cons]
	local beta_cmom = _b[`depvar']
	local tbeta_cmom = _b[`depvar'] / _se[`depvar']
	local r2_ff5cmom = `e(r2_a)'

	clear
	qui set obs 1
	
	qui gen i = `ctr'
	qui gen dep_var = "`depvar'"

	qui gen alpha_ff5 = `alpha_ff5'
	qui gen talpha_ff5 = `talpha_ff5'
	qui gen r2_ff5 = `r2_ff5'

	qui gen alpha_ff5umd = `alpha_ff5umd'
	qui gen talpha_ff5umd = `talpha_ff5umd'
	qui gen r2_ff5umd = `r2_ff5umd'

	qui gen alpha_ff5imom = `alpha_ff5imom'
	qui gen talpha_ff5imom = `talpha_ff5imom'
	qui gen r2_ff5imom = `r2_ff5imom'
		
	qui gen alpha_ff5fmom = `alpha_ff5fmom'
	qui gen talpha_ff5fmom = `talpha_ff5fmom'
	qui gen r2_ff5fmom = `r2_ff5fmom'
	
	qui gen N_ff5imom = `N'
	
	if `ctr'>1 {
		append using "~/Documents/CRSP-Compustat/characteristicmomentumtbl.dta"
		}
	sort i
	save "~/Documents/CRSP-Compustat/characteristicmomentumtbl.dta", replace
	
	clear
	qui set obs 1
	
	qui gen i = `ctr'
	qui gen indep_var = "`depvar'"

	qui gen alpha_ff5cmom = `alpha_ff5cmom'
	qui gen talpha_ff5cmom = `talpha_ff5cmom'
	qui gen beta_cmom = `beta_cmom'
	qui gen tbeta_cmom = `tbeta_cmom'
	qui gen r2_ff5cmom = `r2_ff5cmom'
	
	if `ctr'>1 {
		append using "~/Documents/CRSP-Compustat/characteristicmomentumtbl2.dta"
		}
	sort i
	save "~/Documents/CRSP-Compustat/characteristicmomentumtbl2.dta", replace
	
	restore
	
	}

	
use "~/Documents/CRSP-Compustat/characteristicmomentumtbl.dta", clear

use "~/Documents/CRSP-Compustat/characteristicmomentumtbl2.dta", clear




*===============================================================================
*
* Table 7: Do factor loadings vary across industries and time?
*
*===============================================================================

use "~/Dropbox/Research/Data/CRSP-Compustat/Computations/industryreturns_daily.dta", clear

keep date industryid yyyymmdd mgindustryret
keep if industryid<=20
keep if yyyymmdd>=19630701

merge m:1 date using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_dailyfactors3.dta", nogenerate keepusing(mktrf smb hml rmw cma ltrev rvar qmj bab) keep(1 3)

gen long yyyy = trunc(yyyymmdd/10000)
gen long mo = trunc(yyyymmdd/100) - 100*yyyy 
gen long yyyyqtr = 10 * yyyy + trunc((mo - 1) / 3) + 1

gen long timeindustry = 100 * yyyyqtr + industryid

keep if ~missing(mgindustryret) & ~missing(mktrf) & ~missing(qmj)

drop yyyy mo

* Create an index that numbers all observations within time-industry.
* Use this to resample es within time-industry

sort timeindustry date
gen long idx = 1 if _n==1 | timeindustry~=timeindustry[_n-1]
sort timeindustry date
replace idx = idx[_n-1] + 1 if timeindustry==timeindustry[_n-1]

* Constant across industries and time
regress mgindustryret mktrf
predict e, resid

save "~/Documents/db.dta", replace


local model_i = 0

foreach model in "mktrf" "mktrf smb hml" "mktrf smb hml rmw cma" "mktrf smb hml rmw cma ltrev rvar" "mktrf smb hml rmw cma ltrev rvar qmj bab" {
	
	local model_i = `model_i' + 1
	local spec_i = 0
	
	foreach spec in "" "i.yyyyqtr##c.(`model')" "i.industryid##c.(`model')" "i.timeindustry##c.(`model')" {
			
		use "~/Documents/db.dta", clear
				
		local spec_i = `spec_i' + 1
		
		if "`spec'" ~= "" {
			qui reghdfe e `model', absorb(`spec') resid
			local k = `e(df_a)'
		} 
		else {
			qui regress e `model'			
			local k = `e(df_m)'
		}
		
		local R2 = `e(r2_a)'
		local N = `e(N)'
		
		* jackknife within industry
		matrix R2s = J(63, 3, .)
		
		forvalues j = 1/63 {
			preserve
			qui count
			local N_raw = `r(N)'
			qui drop if idx==`j'			
			qui count
			local D = `N_raw' - `r(N)'

			if "`spec'" ~= "" {
				qui reghdfe e `model', absorb(`spec')
			} 
			else {
				qui regress e `model'			
			}
			
			
			matrix R2s[`j', 1] = `e(r2_a)' 
			matrix R2s[`j', 2] = `N_raw' 
			matrix R2s[`j', 3] = `D' 
			restore
		}
				
		* store results
		clear
		qui svmat double R2s, name(R2s)
		_pctile R2s1, p(2.5)
		local plow = `r(r1)'
		_pctile R2s1, p(97.5)
		local phigh = `r(r1)'
		
		rename R2s1 R2_bs
		rename R2s2 N_raw
		rename R2s3 D
		
		egen double mu = mean(R2_bs)
		gen double R2_bs2 = (R2_bs - mu)^2
		
		collapse (mean) R2_bs N_raw D (sum) R2_bs2 (count) nsamples=D (sd) sd=R2_bs (min) min=R2_bs (max) max=R2_bs
		gen p2_5 = `plow'
		gen p97_5 = `phigh'
		gen model = `model_i'
		gen spec = `spec_i'
		gen R2 = `R2'
		gen N = `N'
		gen k = `k'
		gen se = sqrt(((N_raw - D) / (D * nsamples)) * R2_bs2)
		
		order model spec N

		if `model_i'>1 | `spec_i'>1 {
			append using "~/Documents/bs_results.dta"
		}
		sort model spec
		qui save "~/Documents/bs_results.dta", replace
		
		list
		
	}
}


*===============================================================================
* Quantify estimation uncertainty
*
* Simulate Adjusted R2 statistics when injecting controlled variation in
* factor loadings
*===============================================================================

forvalues outerloop=1/2 {
	
	local model_i = 0

	foreach model in "mktrf" "mktrf smb hml" "mktrf smb hml rmw cma" "mktrf smb hml rmw cma ltrev rvar" "mktrf smb hml rmw cma ltrev rvar qmj bab" {
		
		local model_i = `model_i' + 1
		local spec_i = 0
		
		disp("`model'")
		
		* Estimate loadings for all industries, get the distribution of loadings, and
		* simulate returns
		
		use "~/Documents/db.dta", clear

		qui statsby _b, by(timeindustry) saving("~/Documents/CRSP-Compustat/est_loadings.dta", replace) nodots: regress e `model'

		use "~/Documents/CRSP-Compustat/est_loadings.dta", clear
		drop _b_cons
		foreach var of varlist _b* {
			gen double sd`var' = `var'
		}
		collapse (mean) _b* (sd) sd_*

		expand 20
		gen byte industryid = _n

		* simulate betas
		foreach var of varlist _b* {
			qui summ `var'
			local m = r(mean)
			qui summ sd`var'
			local sd = r(mean)
			qui replace `var' = rnormal(`m', `sd')
		}

		keep industryid _b*

		save "~/Documents/CRSP-Compustat/est_loadings2.dta", replace


		use "~/Documents/db.dta", clear
		qui merge m:1 timeindustry using "~/Documents/CRSP-Compustat/est_loadings.dta", nogenerate

		* take out actual betas * factors
		gen double e2 = e
		foreach var of varlist `model' {
			qui replace e2 = e2 - _b_`var' * `var' 
		}
		drop _b*

		* merge in simulated betas
		qui merge m:1 industryid using "~/Documents/CRSP-Compustat/est_loadings2.dta", nogenerate

		foreach var of varlist `model' {
			qui replace e2 = e2 + _b_`var' * `var' 
		}
		drop _b_*

		save "~/Documents/db2.dta", replace



		
		foreach spec in "" "i.yyyyqtr##c.(`model')" "i.industryid##c.(`model')" "i.timeindustry##c.(`model')" {
				
			use "~/Documents/db2.dta", clear
					
			local spec_i = `spec_i' + 1
			
			if "`spec'" ~= "" {
				qui reghdfe e2 `model', absorb(`spec') resid
				local k = `e(df_a)'
			} 
			else {
				qui regress e2 `model'			
				local k = `e(df_m)'
			}
			
			local R2 = `e(r2_a)'
			local N = `e(N)'
			
			clear
			
			qui set obs 1
			
			gen model = `model_i'
			gen spec = `spec_i'
			gen R2 = `R2'
			gen N = `N'
			gen k = `k'
			
			order model spec N

			if `model_i'>1 | `spec_i'>1 {
				append using "~/Documents/bs_results_tmp.dta"
			}
			sort model spec
			qui save "~/Documents/bs_results_tmp.dta", replace
			
			
		}
	}

	* append
	
	use "~/Documents/bs_results_tmp.dta", clear
	
	gen outerloop = `outerloop'
	
	if `outerloop'>1 {
		append using "~/Documents/bs_results2.dta"
	}
	order outerloop
	qui save "~/Documents/bs_results2.dta", replace
	
}


use "~/Documents/bs_results2.dta", clear
collapse (mean) N R2 k, by(model spec)






* Extract principal components

*===============================================================================
* Extract principal components and measure the amount of cross-sectinal factor
* momentum
*
* Compute time-series factor momentum returns out-of-sample
*
* Initial step: Create simple monthly and daily files with just the factors
*
* Step 1. Estimate PCs from daily data up to month t
* Step 2. Compute scores using monthly data up to month t+1
*         Scale PC variances (up to month t) to match the average variance
*         of raw factors (up to month t)
* Step 3. Compute tsmom return for month t+1
*===============================================================================

local stylectr = 0

foreach factor_style in "factorret" "factorret_in" {

	local stylectr = `stylectr' + 1

	forvalues b_rolling=0/2 {

		* file name for different rolling-specifications
		if `b_rolling'==0 {
			local rname = ""
		}
		else if `b_rolling'==1 {
			local rname = "_rolling"
		}
		else if `b_rolling'==2 {
			local rname = "_srolling"
		}
	
		* Prepare factors: Monthly
		use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_clean.dta", clear

		unique factorid
		local nofactors = `r(unique)'
		
		rename `factor_style' factor

		keep yyyymm factorid factor

		reshape wide factor, i(yyyymm) j(factorid)

		* replace missing factor returns with zeros - this seems to be implied by footnote 16 in the KNS2020

		qui mvencode _all, mv(0) override
		
		save "~/Documents/CRSP-Compustat/fmfactors_clean2.dta", replace


		* Prepare factors: Daily
		use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fmfactors_daily_clean.dta", clear
		
		drop if missing(date)
		
		rename `factor_style' factor
		
		keep date factorid factor 

		reshape wide factor, i(date) j(factorid)

		qui mvencode _all, mv(0) override

		* take 3-day averages to smooth
		*foreach var of varlist factor* {
		*	sort date
		*	qui gen s`var' = (`var'[_n-1] + `var' + `var'[_n+1]) / 3
		*	sort date
		*	qui replace s`var' = (`var' + `var'[_n+1]) / 2 if _n==1
		*	sort date
		*	qui replace s`var' = (`var'[_n-1] + `var') / 2 if _n==_N						
		*}
		*qui drop factor*
		*rename sfactor* factor*
		
		save "~/Documents/CRSP-Compustat/fmfactors_daily_clean2.dta", replace

		local loop_ctr = 0

		forvalues yyyy=1968/2021 {
			forvalues mo=1/12 {
			
				if 100*`yyyy'+`mo'>=202112 {
					continue
				}

				local loop_ctr = `loop_ctr' + 1
				
				* Step 1: Compute PCs from daily data
				* ======
				
				use "~/Documents/CRSP-Compustat/fmfactors_daily_clean2.dta", clear
				
				* Last day of the month? 
				if `mo'<12 {
					local lastday = mdy(`mo'+1,1,`yyyy') - 1
					local firstday = mdy(`mo'+1,1,`yyyy'-10)
					local firstdays = mdy(`mo'+1,1,`yyyy'-5)
				}
				else {
					local lastday = mdy(1,1,`yyyy'+1) - 1
					local firstday = mdy(1,1,`yyyy'-9)
					local firstdays = mdy(1,1,`yyyy'-4)
					}
				
				qui keep if date<=`lastday'
				if `b_rolling'==1 {
					qui keep if date>=`firstday'
				}
				else if `b_rolling'==2 {
					qui keep if date>=`firstdays'
				}
								
				qui pca factor*
				
				* save daily factors
				qui predict pc1-pc`nofactors'
				
				qui gen long yyyymmdd = 10000 * year(date) + 100 * month(date) + day(date)
				qui gen long yyyymm = 100*`yyyy'+`mo'
				
				* up to last five years
				qui gen long monthindex = (year(date) - 1900) * 12 + month(date)
				qui gen long monthindex_today = (`yyyy' - 1900) * 12 + `mo'

				qui keep if monthindex_today - monthindex <= 2
				
				keep yyyymm yyyymmdd pc*
				
				if `loop_ctr'>1 {
					append using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/daily_pc_factors`rname'`stylectr'.dta"
				}
				qui save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/daily_pc_factors`rname'`stylectr'.dta", replace
				
				* Step 2: Compute scores using monthly data
				* ======
						
				use "~/Documents/CRSP-Compustat/fmfactors_clean2.dta", clear
				
				if `mo'<12 {
					local oos_month = 100 * `yyyy' + `mo' + 1
					}
				else {
					local oos_month = 100 * (`yyyy' + 1) + 1
					}

				qui keep if yyyymm<=`oos_month'
				
				
				disp("`oos_month'")
				
				* compute scores

				qui predict pc1-pc`nofactors'
				
				keep yyyymm pc*

				* rescale to 10% annual vol
				foreach var of varlist pc* {
					qui summ `var' if yyyymm<`oos_month'	
					qui gen s`var' = `var' * (0.1 / sqrt(12)) / `r(sd)'
					}
				
				
				* Step 3: Construct factor momentum strategies
				* ======
				
				qui reshape long pc spc, i(yyyymm) j(factorid)

				gen long monthindex = (trunc(yyyymm/100)-1900)*12 + mod(yyyymm,100)

				qui xtset factorid monthindex

				* compute prior-month and prior-year returns 
				qui gen Lpc = L1.pc 
				qui gen Lpc12 = L1.pc + L2.pc + L3.pc + L4.pc + L5.pc + L6.pc + L7.pc + L8.pc + L9.pc + L10.pc + L11.pc + L12.pc 

				qui gen Lspc = L1.spc 
				qui gen Lspc12 = L1.spc + L2.spc + L3.spc + L4.spc + L5.spc + L6.spc + L7.spc + L8.spc + L9.spc + L10.spc + L11.spc + L12.spc 
				
				* we only need the one OOS month
				qui keep if yyyymm==`oos_month'
				
				if `loop_ctr'>1 {
						append using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors`rname'`stylectr'.dta"
					}
				qui save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors`rname'`stylectr'.dta", replace				
				
			}
		}
	}
}
	

local breverse=1

use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors_rolling2.dta", clear
unique factorid

forvalues nofactors=2/41 {

	use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors_rolling2.dta", clear

	* after 41 not full
	keep if factorid<=41
	
	qui keep if yyyymm>=197307
	
	if `breverse'~=1 {
		qui keep if factorid<=`nofactors'
		}
	else {
		qui keep if factorid>41-`nofactors'
		}
		
	bysort yyyymm: egen m = median(Lpc)

	gen     pos = 2 if Lpc > m & ~missing(Lpc,m)
	replace pos = 1 if Lpc < m & ~missing(Lpc,m)

	keep if ~missing(pos)

	collapse (mean) pc spc, by(yyyymm pos)

	qui reshape wide pc spc, i(yyyymm) j(pos)

	*gen csfmom    = spc2 - spc1	
	gen csfmom = pc2 - pc1	

	* scale strategy to 10% annualized vol
	qui summ csfmom
	qui replace csfmom = (0.1 / sqrt(12)) * csfmom / `r(sd)'
	
	* Merge in industry momentum and FF5
	qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/imom_etc.dta", nogenerate keep(1 3)

	* Merge in composite characteristic momentum
	merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/characteristicmomentum_strategies.dta", nogenerate keepusing(composite)
	
	local depvar = "composite"
	* local depvar = "imom"
	
	qui summ yyyymm
	
	forvalues isample=1/3 {
		
		if `isample'==1 {
			local sample_str = ""
			}
		else if `isample'==2 {
			local sample_str = "if yyyymm<=199404"
			}
		else if `isample'==3 {
			local sample_str = "if yyyymm>199404"
			}
			
		qui regress `depvar' mktrf smb hml rmw cma `sample_str'
		local imom_alpha`isample' = _b[_cons]
		local imom_talpha`isample' = _b[_cons] / _se[_cons]
		
		qui regress `depvar' mktrf smb hml rmw cma csfmom `sample_str'
		local imom_alpha_net`isample' = _b[_cons]
		local imom_talpha_net`isample' = _b[_cons] / _se[_cons]

		qui regress csfmom mktrf smb hml rmw cma `sample_str'
		local fmom_alpha`isample' = _b[_cons]
		local fmom_talpha`isample' = _b[_cons] / _se[_cons]
		
		qui regress csfmom mktrf smb hml rmw cma `depvar' `sample_str'
		local fmom_alpha_net`isample' = _b[_cons]
		local fmom_talpha_net`isample' = _b[_cons] / _se[_cons]
	
		}
		
	qui keep if _n==1
	gen nofactors = `nofactors'

	forvalues isample=1/3 {
		foreach res in "imom_alpha" "imom_talpha" "imom_alpha_net" "imom_talpha_net" "fmom_alpha" "fmom_talpha" "fmom_alpha_net" "fmom_talpha_net" {
			gen `res'`isample' = ``res'`isample''
			}
		}
	keep nofactors imom_alpha* imom_talpha* imom_alpha_net* imom_talpha_net* fmom_alpha* fmom_talpha* fmom_alpha_net* fmom_talpha_net* 
	
	if `nofactors'>2 {
		append using "~/Documents/Temp/pcresults.dta"
		}
	qui save "~/Documents/Temp/pcresults.dta", replace
	
	}
	
sort nofactors




*===============================================================================
*
* Table 10: Industry and other forms of characteristic momentum versus momentum 
*           in PC factors
*
*===============================================================================

* Compute momentum in groups of PCs
*use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors_rolling2.dta", clear
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors2.dta", clear

qui keep if yyyymm>=197307

* after 41 not full
keep if factorid<=41

gen     set = 1  if factorid <= 5
replace set = 2  if factorid >  5 & factorid <= 10
replace set = 3  if factorid > 10 & factorid <= 15
replace set = 4  if factorid > 15 & factorid <= 20
replace set = 5  if factorid > 20 & factorid <= 25
replace set = 6  if factorid > 25 & factorid <= 30
replace set = 7  if factorid > 30 & factorid <= 35
replace set = 8  if factorid > 35 

drop if missing(set)

bysort yyyymm set: egen m = median(Lpc)

gen     pos = 2 if Lpc > m & ~missing(Lpc,m)
replace pos = 1 if Lpc < m & ~missing(Lpc,m)

keep if ~missing(pos)

collapse (mean) pc, by(yyyymm set pos)

qui reshape wide pc, i(yyyymm pos) j(set)

rename pc? pc?p

qui reshape wide pc?p, i(yyyymm) j(pos)

forvalues p=1/8 {
	gen csfmom`p' = pc`p'p2 - pc`p'p1	
	}

keep yyyymm csfmom*

* scale strategies to 10% annualized vol
foreach var of varlist csfmom* {
	qui summ `var'
	qui replace `var' = (0.1 / sqrt(12)) * `var' / `r(sd)'
	}

keep if ~missing(csfmom1)

* Merge in industry momentum and FF5
qui merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/imom_etc.dta", nogenerate keep(1 3)

* Merge in composite characteristic momentum
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/characteristicmomentum_strategies.dta", nogenerate keepusing(composite)

estimates clear

* industry & composite characteristic momentum by itself
qui regress imom mktrf smb hml rmw cma 
estimates store regrd0
qui regress composite mktrf smb hml rmw cma 
estimates store regre0

forvalues iset=1/8 {
	qui  regress csfmom`iset' mktrf smb hml rmw cma 
	estimates store regra`iset'
	qui  regress csfmom`iset' mktrf smb hml rmw cma csfmom1
	estimates store regrb`iset'
	qui  regress csfmom1 mktrf smb hml rmw cma csfmom`iset'
	estimates store regrc`iset'
	qui  regress imom mktrf smb hml rmw cma csfmom`iset'
	estimates store regrd`iset'
	qui  regress composite mktrf smb hml rmw cma csfmom`iset'
	estimates store regre`iset'
	}

qui  regress csfmom1 mktrf smb hml rmw cma csfmom2 csfmom3 csfmom4 csfmom5 csfmom6 csfmom7 csfmom8
estimates store regrc9
	
esttab regra?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons)
esttab regrb?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons csfmom1) 
esttab regrc?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons csfmom*) 
esttab regrd?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons csfmom*) 	

esttab regre?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons csfmom*) 


* Does PC factor momentum explain characteristics momentum?
keep yyyymm csfmom? mktrf smb hml rmw cma 
merge 1:1 yyyymm using "~/Documents/CRSP-Compustat/characteristicmomentum_strategies.dta", nogenerate

* spanning regressions
local ctr = 0

foreach depvar of varlist imom imom2 sz_mom bm_mom sz_bm_mom kmeans25 ward25 kmeans25_big ward25_big composite { 
	
	local ctr = `ctr' + 1
	
	qui regress `depvar' mktrf smb hml rmw cma 
	estimates store regra`ctr'
 	qui regress `depvar' mktrf smb hml rmw cma csfmom1
	estimates store regrb`ctr'
	
}

esttab regra*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons)
esttab regrb*, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons csfmom1)







*===============================================================================
*
* Calculation/Processing: Systematic and idiosyncratic industries
*
*===============================================================================

foreach lookback in "3" "60" {
	forvalues iset=1/10 {
		
		if `lookback' ~= 3 & `iset'>=6 {
			continue
		}

		if `lookback' == 3 & `iset'<6 {
			continue
		}
		
		if `iset'==1 {
			local factorlist = "mktrf"
		}
		else if `iset'==2 {
			local factorlist = "mktrf smb hml"
		}
		else if `iset'==3 {
			local factorlist = "mktrf smb hml rmw cma"
		}
		else if `iset'==4 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar"
		}
		else if `iset'==5 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar qmj bab"
		}
		else if `iset'==6 {
			local factorlist = "pc1"
		}
		else if `iset'==7 {
			local factorlist = "pc1 pc2"
		}
		else if `iset'==8 {
			local factorlist = "pc1 pc2 pc3"
		}
		else if `iset'==9 {
			local factorlist = "pc1 pc2 pc3 pc4"
		}
		else if `iset'==10 {
			local factorlist = "pc1 pc2 pc3 pc4 pc5"
		}
		
		* number of factors
		local vctr = 0
		foreach var in `factorlist' {
			local vctr = `vctr' + 1
		}
		local nofactors = `vctr'
		
		
		* select monthly factors
		if `iset'<=5 {
			use "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_monthlyfactors3.dta", clear
			merge 1:1 yyyymm using "~/Dropbox/Research/Data/CRSP-Compustat/Computations/mgindustryreturns_monthly.dta", nogenerate

			qui keep if ~missing(hml)

			keep if yyyymm>=196307
			sort yyyymm

			keep yyyymm mg* rf `factorlist'


		}
		else {
			use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/pcfactors_rolling1.dta", clear
			keep yyyymm factorid pc Lpc
			qui keep if factorid<=`nofactors'
			qui reshape wide pc Lpc, i(yyyymm) j(factorid)
			merge 1:1 yyyymm using "~/Dropbox/Research/Data/CRSP-Compustat/Computations/mgindustryreturns_monthly.dta", nogenerate
			merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_monthlyfactors3.dta", nogenerate keep(1 3) keepusing(rf)						
		}

		
		* specify the factors to retain
		local vctr = 0
		foreach var of varlist `factorlist' {
			local vctr = `vctr' + 1
			disp("`var' `vctr'")
			rename `var' regr`vctr'
			if `iset'>=6 {
				rename L`var' Lregr`vctr'
			}
		}
		
		save "~/Documents/fm_monthly.dta", replace
		
		* select daily factors	
		if `iset'<=5 {
			use "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_dailyfactors3.dta", clear
			merge 1:1 yyyymmdd using "~/Dropbox/Research/Data/CRSP-Compustat/Computations/mgindustryreturns_daily.dta", nogenerate

			qui keep if ~missing(hml)

			gen long yyyymm = trunc(yyyymmdd/100)

			keep if yyyymm>=196307
			sort yyyymmdd

			keep yyyymmdd yyyymm `factorlist' mg* rf
			
		}
		else {
			use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/daily_pc_factors_rolling1.dta", clear
			keep yyyymm yyyymmdd `factorlist'
			merge m:1 yyyymmdd using "~/Dropbox/Research/Data/CRSP-Compustat/Computations/mgindustryreturns_daily.dta", nogenerate keep(1 3)
			merge m:1 yyyymmdd using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_dailyfactors3.dta", nogenerate keep(1 3) keepusing(rf)
			
		}
		
		* specify the factors to retain
		local vctr = 0
		foreach var of varlist `factorlist' {
			local vctr = `vctr' + 1
			rename `var' regr`vctr'
		}
	
		save "~/Documents/fm_daily.dta", replace

		
		* Decompose industry returns

		use "~/Documents/fm_monthly.dta", clear
		sort yyyymm
		qui keep if ~missing(regr1)
		gen long monthindex = _n
		tsset monthindex
		summ monthindex
		local maxt = `r(max)'

		local begt = `lookback' + 1
		
		* Loop through MG industries
		forvalues ind=1/20 {
			
			qui gen double smg_`ind' = 0 if ~missing(mg_`ind') & monthindex>`lookback'
			qui gen double lagged_smg_`ind' = 0 if ~missing(mg_`ind') & monthindex>=`lookback'
			qui gen double R2_`ind' = 0 if ~missing(mg_`ind') & monthindex>`lookback'
			
			qui replace mg_`ind' = mg_`ind' - rf
			
			* loop through months (max monthindex)
			forvalues t=`begt'/`maxt' {
				
				* find bounds
				qui summ yyyymm if monthindex>=`t'-`lookback' & monthindex<=`t'-1

				if `iset'<=5 {
					preserve
					use "~/Documents/fm_daily.dta", clear
					qui keep if yyyymm>=`r(min)' & yyyymm<=`r(max)' 
					qui replace mg_`ind' = mg_`ind' - rf
					qui regress mg_`ind' regr*
					restore		
					forvalues j=1/`nofactors' {
						qui replace smg_`ind' = smg_`ind' + _b[regr`j'] * regr`j' if monthindex==`t'
						qui replace lagged_smg_`ind' = lagged_smg_`ind' + _b[regr`j'] * regr`j' if monthindex==`t'-1
					}
					qui replace R2_`ind' = `e(r2_a)' if monthindex==`t'					
				}
				else {
					preserve
					use "~/Documents/fm_daily.dta", clear
					qui keep if yyyymm==`r(max)' 
					qui replace mg_`ind' = mg_`ind' - rf
					qui regress mg_`ind' regr*
					restore		
					forvalues j=1/`nofactors' {
						qui replace smg_`ind' = smg_`ind' + _b[regr`j'] * regr`j' if monthindex==`t'
						qui replace lagged_smg_`ind' = lagged_smg_`ind' + _b[regr`j'] * F.Lregr`j' if monthindex==`t'-1
						
						* This last statement requires an explanation. In the PC-data, variables pc and Lpc are both reported
						* on the row for month t. pc is this month's return, Lpc is the previous month's return based on the same
						* rotation. So, if we want to know what was the return in month t-1 for the rotation that is applied to
						* month t, we need to grab next month's Lpc. This is what happens with the F.Lregr statement.
						
					}					
					qui replace R2_`ind' = `e(r2_a)' if monthindex==`t'
				}
				
				
			}
		}
		
		gen iset = `iset'
		gen factorset = "`factorlist'"
		
		if `iset'>1 {
			append using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic`lookback'.dta"
			}
		save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic`lookback'.dta", replace
	}
}



*===============================================================================
*
* Calculation/Processing: Systematic and idiosyncratic industry momentum
* 						  strategies
*
*
* Note: Choose between 3/60 files to use different estimation periods for factor
* loadings
*===============================================================================

forvalues iset=1/10 {
	
	display(`iset')
	
	use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic3.dta", clear
	*use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic60.dta", clear
		
	keep if iset==`iset'
	
	duplicates drop
	
	rename mg_* ret*
	rename smg_* sret*
	rename lagged_smg_* lagged_sret*

	keep yyyymm monthindex ret* sret* lagged_sret*

	reshape long ret sret lagged_sret, i(yyyymm) j(industry) 
		
	xtset industry monthindex
	
	* residual industry 
	gen eret = ret - sret
	
	* prior month's return 	
	gen Lret = L.ret
	gen Lsret = L.lagged_sret
	gen Leret = Lret - Lsret
	
	* 1. rank by industry return
	bysort monthindex: egen r = rank(Lret)
	bysort monthindex: egen m = median(r)

	gen byte Q1 = 1 if r<m & ~missing(r,m)
	replace	 Q1 = 2 if r>m & ~missing(r,m)

	drop r m

	* 2. rank by systematic industry return
	bysort monthindex: egen r = rank(Lsret)
	bysort monthindex: egen m = median(r)

	gen byte Q2 = 1 if r<m & ~missing(r,m)
	replace	 Q2 = 2 if r>m & ~missing(r,m)

	drop r m

	* 3. rank by residual industry return
	bysort monthindex: egen r = rank(Leret)
	bysort monthindex: egen m = median(r)

	gen byte Q3 = 1 if r<m & ~missing(r,m)
	replace	 Q3 = 2 if r>m & ~missing(r,m)

	drop r m

	keep if ~missing(Q1,Q2,Q3,sret)

	* 1. Industry sorted by industry
	preserve
	collapse (mean) ret eret sret, by(yyyymm Q1)
	reshape wide ret eret sret, i(yyyymm) j(Q1)
	gen imom = ret2 - ret1
	keep yyyymm imom
	qui save "~/Documents/Temp/tmp.dta", replace
	restore

	* 2 & 4. Industry and systematic industry sorted by systematic industry
	preserve
	collapse (mean) ret sret, by(yyyymm Q2)
	reshape wide ret sret, i(yyyymm) j(Q2)
	gen smom = sret2 - sret1
	keep yyyymm smom
	qui merge 1:1 yyyymm using "~/Documents/Temp/tmp.dta", nogenerate 
	qui save "~/Documents/Temp/tmp.dta", replace
	restore

	* 3. Industry sorted by residual industry
	collapse (mean) eret, by(yyyymm Q3)
	reshape wide eret, i(yyyymm) j(Q3)
	gen emom = eret2 - eret1
	keep yyyymm emom
	qui merge 1:1 yyyymm using "~/Documents/Temp/tmp.dta", nogenerate 

	order yyyymm imom smom emom

	gen byte iset = `iset'
	
	if `iset'>1 {
		append using "~/Dropbox/Research/Factor Momentum 2/Data (derived)/imoms.dta"
		}
	
	qui save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/imoms.dta", replace
	
	}


	
*===============================================================================
*	
* Table 8: Momentum in systematic industries vs. industry momentum
*
*===============================================================================
	
* Note: Market return in November 1964 is 0.0000 in Ken French's file -- that is, it is zero to decimals, 0.00%.
*       Because of this, all systematic industries in the CAPM specification have the same return, and we cannot 
*       sort these into portfolios. This is why the CAPM specification is missing the December 1964 observation.
	
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/imoms.dta", clear

qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", nogenerate

qui keep if ~missing(imom,smom)


* Panel A: Industry momentum
* ==========================

estimates clear

qui regress imom mktrf smb hml rmw cma if iset==2

estimates store regra0

forvalues iset=1/5 {
	
	qui regress imom mktrf smb hml rmw cma smom if iset==`iset'
	estimates store regra`iset'

	qui regress imom mktrf smb hml rmw cma emom if iset==`iset'
	estimates store regrb`iset'
	
	}
	
esttab regra?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons smom) 
esttab regrb?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons emom) 
	
	
* Panel B: Systematic industries
* ==============================

estimates clear

forvalues iset=1/5 {
	
	qui regress smom mktrf smb hml rmw cma if iset==`iset'
	estimates store regra`iset'

	qui regress smom mktrf smb hml rmw cma imom if iset==`iset'
	estimates store regrb`iset'
	
	}
	
esttab regra?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar  keep(_cons) 
esttab regrb?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar  keep(_cons imom) 


* Appendix: PC factors (if 3-month estimation period)
* ==========================
	
use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/imoms.dta", clear

qui merge m:1 yyyymm using "~/Dropbox/Research/Anomalies/Fama-French factors/fffactors.dta", nogenerate

qui keep if ~missing(imom,smom)


* Panel A: Industry momentum
* ==========================

estimates clear

qui regress imom mktrf smb hml rmw cma if iset==2

estimates store regra0
local ctr = 0

forvalues iset=6/10 {

	local ctr = `ctr' + 1
	
	qui regress imom mktrf smb hml rmw cma smom if iset==`iset'
	estimates store regra`ctr'

	qui regress imom mktrf smb hml rmw cma emom if iset==`iset'
	estimates store regrb`ctr'
	
	}
	
esttab regra?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons smom) 
esttab regrb?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar keep(_cons emom) 
	
	
* Panel B: Systematic industries
* ==========================

estimates clear
local ctr = 0

forvalues iset=6/10 {
	
	local ctr = `ctr' + 1
	
	qui regress smom mktrf smb hml rmw cma if iset==`iset'
	estimates store regra`ctr'

	qui regress smom mktrf smb hml rmw cma imom if iset==`iset'
	estimates store regrb`ctr'
	
	}
	
esttab regra?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar  keep(_cons) 
esttab regrb?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar  keep(_cons imom) 



*===============================================================================
* Table 11: Autocovariance matrix and momentum for industries
*	  	    Autocovariance matrix and momentum for factors
*===============================================================================

local industry = 1

if `industry'==1 {
	
	use "~/Dropbox/Research/Data/CRSP-Compustat/Computations/industryreturns.dta", clear
		
	rename mgindustryret ret				
	keep if ~missing(ret)
	keep if yyyymm>=196307
	keep dateindex industryid ret

	reshape wide ret, i(dateindex) j(industryid)

}
else {
	
	use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/temp_factors2.dta", clear
	
	keep factorid yyyymm factorret
	
	keep if yyyymm>=196307
	
	rename factorret ret
	
	reshape wide ret, i(yyyymm) j(factorid)
	
	gen long dateindex = (trunc(yyyymm/100) - 1900) * 12 + mod(yyyymm,100)
	
	keep if yyyymm>=196307
	drop yyyymm
	
}

tsset dateindex

foreach var of varlist ret* {
	qui replace `var' = 100 * `var'
	qui gen double L`var' = L.`var'
}

forvalues bs=0/500 {
	
	preserve

	if `bs'>0 {
		bsample
	}
	
	local auto = 0
	local cross = 0
	local nf = 0
	
	foreach fct of varlist ret* {
		local nf = `nf' + 1
		foreach fct2 of varlist ret* {
			qui corr `fct' L`fct2', cov
			if "`fct'" == "`fct2'" {
				local auto = `auto' + `r(cov_12)'
			}
			else {
				local cross = `cross' + `r(cov_12)'			
			}
		}
	}

	* linear momentum strategy return
	qui egen m = rowmean(Lret*)
	qui gen mom = 0
	foreach var of varlist ret* {
		qui replace mom = mom + (1 / `nf') * (L`var' - m) * `var'
	}
		
	collapse (mean) mom (sd) sd_mom=mom
	
	gen long bs = `bs'
	
	gen double autocov = `auto' / `nf'
	gen double crosscov = `cross' / (`nf' * (`nf' - 1))

	gen double autocov_ret = `auto' * (`nf' - 1) / (`nf' ^ 2)
	gen double crosscov_ret = `cross' * (-1) / (`nf' ^ 2)
	
	if `bs'>0 {
		append using "~/Documents/acovs.dta"
	}
	qui save "~/Documents/acovs.dta", replace
	
	restore
	
}

use "~/Documents/acovs.dta", clear

foreach var of varlist mom autocov_ret crosscov_ret {
	qui replace `var' = `var' * (0.1 / sqrt(12)) / sd_mom
}

gen mom_2 = autocov_ret + crosscov_ret
gen meaneff = mom - mom_2

summ, sep(0)

list if bs==0

corr autocov_ret crosscov_ret


*===============================================================================
* Table 11: Autocovariance matrix and momentum for individual stocks
*===============================================================================

use "~/Dropbox/Research/Data/CRSP-Compustat/merged_crsp_compustat.dta", clear

keep permno yyyymm dateindex retnm r1_1

rename r1_1 Lret
rename retnm ret

qui replace ret = 100 * ret
qui replace Lret = 100 * Lret

qui gen long yyyymm2 = yyyymm + 1 if mod(yyyymm,100) < 12
qui replace yyyymm2 = (trunc(yyyymm / 100) + 1) * 100 + 1 if mod(yyyymm,100) == 12

drop yyyymm
rename yyyymm2 yyyymm

keep if yyyymm>=196307

keep if ~missing(ret,Lret)

sort permno yyyymm

save "~/Documents/stockreturns.dta", replace

* save stock list

use "~/Documents/stockreturns.dta", clear
keep permno
gen nobs = 1
collapse (count) nobs, by(permno)
save "~/Documents/stocklist.dta", replace

* select random stocks
forvalues bs=1/100 {
	
	disp(`bs')
	
	local nstocks = 2000

	use "~/Documents/stocklist.dta", clear
	*qui keep if nobs>=60
	gen e = runiform()
	sort e
	qui keep if _n<=`nstocks'
	gen long permno2 = _n
	drop e nobs
	sort permno

	qui merge 1:m permno using "~/Documents/stockreturns.dta", keep(3) sorted nogenerate
		
	* sequential permnos
	drop yyyymm permno
	rename permno2 permno

	qui reshape wide ret Lret, i(dateindex) j(permno)

	qui bsample
	
	order dateindex ret* 

	* compute pairwise correlations
	qui pwcorr ret* Lret*
	matrix rho = r(C)

	* compute standard deviations
	qui tabstat ret* Lret*, stat(sd) save
	matrix s = r(StatTotal)

	egen n = rownonmiss(ret*)

	qui gen long n_diag = 0
	qui gen long n_off = 0

	gen double TR = 0 
	gen double OFF = 0

	* loop to get elements for this row
	forvalues p=1/`nstocks' {
		forvalues p2=1/`nstocks' {
			
			local L = `p2' + `nstocks'
			
			qui gen double tmp = rho[`p', `L'] * s[1,`p'] * s[1,`L'] if ~missing(ret`p',Lret`p2')
					
			if `p' == `p2' {
				qui replace TR = TR + tmp if ~missing(tmp)
				qui replace n_diag = n_diag + 1 if ~missing(tmp)
			}
			else {
				qui replace OFF = OFF + tmp if ~missing(tmp)
				qui replace n_off = n_off + 1 if ~missing(tmp)
			}
			drop tmp
			
		}
	}

	qui gen autocov = TR / n_diag
	qui gen crosscov = OFF / n_off

	qui gen autocov_ret = TR * (n_diag - 1) / (n_diag ^ 2)

	* get the number of diagonal elements based on the no. of off-diagonal (could be diff. if a stock survives only briefly)
	qui gen n_off2 = (1 + sqrt(1 + 4 * n_off)) / 2

	qui gen crosscov_ret = OFF * (-1) / (n_off2 ^ 2)

	* linear momentum strategy return
	qui egen double m = rowmean(Lret*)
	qui gen double mom = 0
	foreach var of varlist ret* {
		qui replace mom = mom + (1 / n) * (L`var' - m) * `var' if ~missing(`var')
	}
			
	drop ret* Lret*

	collapse (mean) autocov* crosscov* mom n (sd) sd_mom=mom	
	
	gen bs = `bs'
	order bs
	
	if `bs'>1 {
		append using "~/Dropbox/Research/Factor Momentum 2/acovs_single.dta"
	}
	qui save "~/Dropbox/Research/Factor Momentum 2/acovs_single.dta", replace
	
}


use "~/Dropbox/Research/Factor Momentum 2/acovs_single.dta", clear

foreach var of varlist mom autocov_ret crosscov_ret {
	qui replace `var' = `var' * (10 / sqrt(12)) / sd_mom
}

gen mom_2 = autocov_ret + crosscov_ret
gen meaneff = mom - mom_2

order mom sd_mom bs autocov crosscov autocov_ret crosscov_ret mom_2 meaneff

summ, sep(0)






*===============================================================================
*
* Calculation/Processing: Systematic and idiosyncratic stocks
*
*===============================================================================

local factorlist_full = "mktrf smb hml rmw cma ltrev rvar qmj bab"		

* Create monthly file with stocks and factors
use "~/Dropbox/Research/Data/CRSP-Compustat/merged_crsp_compustat.dta", clear

keep yyyymm permno r1_1 dateindex
qui keep if yyyymm>=196307 & ~missing(r1_1)
rename r1_1 ret
rename dateindex monthindex

merge m:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_monthlyfactors3.dta", nogenerate keep(1 3) keepusing(rf `factorlist_full')
qui keep if ~missing(mktrf)

qui gen double retrf = ret - rf
drop ret rf
	
qui gen byte exist = 1
	
sort permno

keep if yyyymm>=200501
save "~/Documents/fm_monthly_small.dta", replace


* create a file with daily returns and factors	
use "~/Dropbox/Research/Data/CRSP-Compustat/simple_dailyreturns.dta", clear
keep permno date ret yyyymm
qui keep if yyyymm>=196307 & ~missing(ret)
qui gen long yyyymmdd = 10000 * year(date) + 100 * month(date) + day(date)
drop date

qui merge m:1 permno yyyymm using "~/Documents/fm_monthly.dta", nogenerate keep(3) keepusing(exist)
drop exist
	
merge m:1 yyyymmdd using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_dailyfactors3.dta", nogenerate keepusing(rf `factorlist_full')
qui keep if ~missing(mktrf) & ~missing(ret)				

qui gen double retrf = ret - rf
drop ret rf

keep if yyyymm>=200501

save "~/Documents/fm_daily_small.dta", replace

* Decompose stock returns

use "~/Documents/fm_monthly_small.dta", clear
drop exist
summ monthindex
local maxt = `r(max)'
local begt = `r(min)' + 3

forvalues iset=1/5 {
	qui gen double sret`iset' = 0 if ~missing(retrf) & monthindex>=`begt'
	qui gen double lagged_sret`iset' = 0 if ~missing(retrf) & monthindex>=`begt'-1
	qui gen double R2_v`iset' = 0 
}
qui gen double N = 0 


*forvalues t=`begt'/`maxt' {
forvalues t=`maxt'(-1)`begt' {

	display(`t')
	
	* find bounds
	qui summ yyyymm if monthindex>=`t'-3 & monthindex<=`t'-1
	
	preserve
	use "~/Documents/fm_daily_small.dta", clear
	qui keep if yyyymm>=`r(min)' & yyyymm<=`r(max)' 
	
	forvalues iset=1/5 {
		
		if `iset'==1 {
			local factorlist = "mktrf"
		}
		else if `iset'==2 {
			local factorlist = "mktrf smb hml"
		}
		else if `iset'==3 {
			local factorlist = "mktrf smb hml rmw cma"
		}
		else if `iset'==4 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar"
		}
		else if `iset'==5 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar qmj bab"
		}

		qui statsby _b N=e(N) R2=e(r2_a), by(permno) saving("~/Documents/CRSP-Compustat/betas`iset'_small.dta", replace) nodots: regress retrf `factorlist'
	}
		
	restore
	
	forvalues iset=1/5 {
		
		sort permno
		
		qui merge m:1 permno using "~/Documents/CRSP-Compustat/betas`iset'_small.dta", nogenerate sorted
				
		if `iset'==1 {
			local factorlist = "mktrf"
			qui replace N = _eq2_N if monthindex==`t'				
		}
		else if `iset'==2 {
			local factorlist = "mktrf smb hml"
		}
		else if `iset'==3 {
			local factorlist = "mktrf smb hml rmw cma"
		}
		else if `iset'==4 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar"
		}
		else if `iset'==5 {
			local factorlist = "mktrf smb hml rmw cma ltrev rvar qmj bab"
		}
		
		foreach factor in `factorlist' {
			qui replace sret`iset' = sret`iset' + _b_`factor' * `factor' if monthindex==`t'
			qui replace lagged_sret`iset' = lagged_sret`iset' + _b_`factor' * `factor' if monthindex==`t'-1
		}
		qui replace R2_v`iset' = _eq2_R2 if monthindex==`t'
		
		drop _b_* _eq2*		
	
	}

	if mod(`t',12) == 0 {
		qui compress
		display("Saving")
		save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic_stocks_tmp2.dta", replace
	}
	
}


save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic_stocks.dta", replace


*===============================================================================
* Create a smaller version
*===============================================================================

use "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic_stocks.dta", clear

local factorlist_full = "mktrf smb hml rmw cma ltrev rvar qmj bab"		

drop `factorlist_full' 

* Put lagged_sret on the correct line

xtset permno monthindex

forvalues iset = 1/5 {
	qui gen Lsret`iset' = L.lagged_sret`iset'
	drop lagged_sret`iset'
}

drop monthindex


forvalues iset = 1/5 {
	qui replace sret`iset' = . if R2_v`iset'<0 | N<21 | missing(N)
	qui replace Lsret`iset' = . if R2_v`iset'<0 | N<21 | missing(N)
	qui replace R2_v`iset' = . if R2_v`iset'<0 | N<21 | missing(N)
}

order permno yyyymm sret? Lsret? R2*

sort permno yyyymm

save "~/Documents/fm_monthly_systematic_stocks_done.dta", replace





 
 
* PCs as systematic factors
	
* Create monthly file with stocks and factors
use "~/Documents/Temp/pcfactors1.dta", clear
keep yyyymm factorid pc Lpc
qui keep if factorid<=5
qui reshape wide pc Lpc, i(yyyymm) j(factorid)
merge 1:1 yyyymm using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_monthlyfactors3.dta", nogenerate keep(1 3) keepusing(rf)							
qui save "~/Documents/Temp/pcfactors_tmp.dta", replace		
	
use "~/Dropbox/Research/Data/CRSP-Compustat/merged_crsp_compustat.dta", clear
qui keep if size_cat>1
keep yyyymm permno r1_1 dateindex
qui keep if yyyymm>=196307 & ~missing(r1_1)
rename r1_1 ret
rename dateindex monthindex

merge m:1 yyyymm using "~/Documents/Temp/pcfactors_tmp.dta", nogenerate keep(1 3)
qui keep if ~missing(pc1)

qui gen double retrf = ret - rf
drop ret rf
	
qui gen byte exist = 1
		
save "~/Documents/fm_monthly.dta", replace

	
* daily returns
use "~/Dropbox/Research/Data/CRSP-Compustat/simple_dailyreturns.dta", clear
keep permno date ret
qui gen long yyyymmdd = 10000 * year(date) + 100 * month(date) + day(date)
qui keep if yyyymm>=19630701 & ~missing(ret)
drop date

qui gen long yyyymm = trunc(yyyymmdd/100)
qui merge m:1 permno yyyymm using "~/Documents/fm_monthly.dta", nogenerate keep(3) keepusing(exist)
drop exist yyyymm

merge m:1 yyyymmdd using "~/Dropbox/Research/Factor Momentum 2/Data/Factor and Industry Data/clean/fm_dailyfactors3.dta", nogenerate keep(1 3) keepusing(rf)
qui gen double retrf = ret - rf
drop ret rf

sort yyyymmdd

qui save "~/Documents/stocks_tmp.dta", replace
	
	
use "~/Documents/CRSP-Compustat/daily_pc_factors1.dta", clear
keep yyyymm yyyymmdd pc1-pc5

qui save "~/Documents/daily_pcfactors_tmp.dta", replace

	
* Decompose stock returns

use "~/Documents/fm_monthly.dta", clear
drop exist
summ monthindex
local maxt = `r(max)'
local begt = `r(min)' + 3

forvalues k=1/5 {
	qui gen double sret`k' = 0 
	qui gen double lagged_sret`k' = 0 
}
qui gen double N = 0 	

sort permno

forvalues t=`begt'/`maxt' {

	display(`t')
	
	* find bounds
	qui summ yyyymm if monthindex>=`t'-3 & monthindex<=`t'-1
	
	preserve
	
	use "~/Documents/daily_pcfactors_tmp.dta", clear
	qui keep if yyyymm==`r(max)' 
	drop yyyymm
	
	sort yyyymmdd
	qui merge 1:m yyyymmdd using "~/Documents/stocks_tmp.dta", nogenerate sorted keep(3)
	
	forvalues k=1/5 {
		qui statsby _b N=e(N), by(permno) saving("~/Documents/CRSP-Compustat/betas`k'.dta", replace) nodots: regress retrf pc1-pc`k'		
	}
	
	restore
	
	forvalues k=1/5 {
		sort permno
		qui merge m:1 permno using "~/Documents/CRSP-Compustat/betas`k'.dta", nogenerate sorted
		forvalues j=1/`k' {
			qui replace sret`k' = sret`k' + _b_pc`j' * pc`j' if monthindex==`t'
			qui replace lagged_sret`k' = lagged_sret`k' + _b_pc`j' * Lpc`j' if monthindex==`t'
		}
		if `k'==1 {
			qui replace N = _eq2_N if monthindex==`t'			
		}
		drop _b_* _eq2*
	}
					
}

drop pc* Lpc*

save "~/Dropbox/Research/Factor Momentum 2/Data (derived)/fm_monthly_systematic_stocks_pc.dta", replace





*===============================================================================
* Table 12, Panel A: Quantify the amount of systematic risk
*===============================================================================

use "~/Documents/fm_monthly_systematic_stocks_done.dta", clear
keep yyyymm retrf sret?

* normalize - take out monthly FEs
areg retrf, absorb(yyyymm)
predict e, resid

estimates clear
forvalues iset = 1/5 {
	qui areg e sret`iset', absorb(yyyymm) cluster(yyyymm)
	estimates store regr`iset'
}

esttab regr?, b(5) t(3) scalars(N r2_a) sfmt(0 5) noparentheses nogaps nostar

forvalues iset = 1/5 {
	qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/sysrisk`iset'.dta", replace) nodots: regress retrf sret`iset'		
}

forvalues iset = 1/5 {
	use "~/Documents/CRSP-Compustat/sysrisk`iset'.dta", clear
	rename _b_sret? b
	rename _eq2_R2 r2
	rename _eq2_N N
	collapse (mean) b r2 N (semean) se_b=b se_r2=r2 
	gen byte factormodel = `iset'
	if `iset'>1 {
		append using "~/Documents/CRSP-Compustat/sysrisk.dta"
		order factormodel
		sort factormodel
	}
	save "~/Documents/CRSP-Compustat/sysrisk.dta", replace
}

	
*===============================================================================
* Table 12, Panel B: Lead-lag relationships
*===============================================================================
local iset = 5

use "~/Dropbox/Research/Data/CRSP-Compustat/merged_crsp_compustat.dta", clear
keep yyyymm permno r1_1 dateindex
rename r1_1 ret
sort permno yyyymm
merge 1:1 permno yyyymm using "~/Documents/fm_monthly_systematic_stocks_done.dta", nogenerate sorted keep(1 3) keepusing(sret`iset' Lsret`iset')

xtset permno dateindex
gen Lret = L.ret

rename sret? sret
rename Lsret? Lsret

drop dateindex permno

keep if ~missing(ret,Lret,sret,Lsret)

* run regressions to get the lagged residual return

qui statsby _b, by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_res.dta", replace) nodots: regress Lret Lsret
qui merge m:1 yyyymm using "~/Documents/CRSP-Compustat/fmb_res.dta", nogenerate
gen double Lret_e = Lret - _b_cons - _b_Lsret * Lsret
drop _b*

* no estimation for today's residual (so that it is tradeable)

gen double ret_e = ret - sret

qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock1.dta", replace) nodots: regress ret Lret
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock2.dta", replace) nodots: regress ret Lsret Lret_e

qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock3.dta", replace) nodots: regress sret Lret
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock4.dta", replace) nodots: regress sret Lsret 
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock5.dta", replace) nodots: regress sret Lret_e
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock6.dta", replace) nodots: regress sret Lsret Lret_e

qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock7.dta", replace) nodots: regress ret_e Lret
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock8.dta", replace) nodots: regress ret_e Lsret 
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock9.dta", replace) nodots: regress ret_e Lret_e
qui statsby _b N=e(N) R2=e(r2_a), by(yyyymm) saving("~/Documents/CRSP-Compustat/fmb_systematic_stock10.dta", replace) nodots: regress ret_e Lsret Lret_e

		
forvalues j=1/10 {
	use "~/Documents/CRSP-Compustat/fmb_systematic_stock`j'.dta", clear
	drop _b_cons
	foreach var of varlist _b* {
		gen double se`var' = `var'
	}
	collapse (mean) _b* N=_eq2_N R2=_eq2_R2 (semean) se_*
	gen byte model = `j'
	if `j'>1 {
		append using "~/Documents/CRSP-Compustat/sysrisk.dta"
		order model
		sort model
	}
	qui save "~/Documents/CRSP-Compustat/sysrisk.dta", replace
}

order model *Lret *Lsret *Lret_e 



